package com.example.controller;

import com.example.service.ContractService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 生成电子合同
 * @Author runto
 * @Date 2023/10/22 10:34
 * @PackageName:com.example.controller
 * @ClassName: ContractController
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/contract")
public class ContractController {

    @Autowired
    private ContractService contractService;

    public static final String YYYYMMDDHHMISS="yyyyMMddHHmmss";

    private static final Logger logger = LoggerFactory.getLogger(FileController.class);

    @ApiOperation(value = "电子合同", notes = "1.生成pdf合同2.添加签章")
    @RequestMapping(value = "/creatContract", method = RequestMethod.POST)
    public void creatContract(HttpServletRequest request) throws Exception {
        String savePath = request.getSession().getServletContext().getRealPath("pdfTemple");
        String fileName = "合同D"+getDateFormat(new Date(),YYYYMMDDHHMISS)+"-temp"+"."+"pdf";
        String HTML = "contract.html";
        logger.info("存放生成临时文件的路径为:{},文件名为:{},模板名称为：{}",savePath,fileName,HTML);

        contractService.saveContract(savePath,fileName,HTML);
    }

    public static String getDateFormat(Date date, String pattern) {
        DateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }
}
